package com.cafe.action.admin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.exception.CafeException;
import com.malamute.core.action.BaseAction;
import com.malamute.core.constant.SystemConstant;
import com.malamute.core.constant.SystemContex;
import com.malamute.core.exception.BusinessException;
import com.malamute.core.exception.SystemException;
import com.malamute.system.dao.UserDao;
import com.malamute.system.entity.User;
import com.malamute.system.service.UserService;

/**
 * @Title:loginAction.java
 * @Package com.cafe.action
 * @Description 员工登录，后台登陆Action
 * @author AceGuan gjybst@163.com
 * @date 2012-1-30 下午12:49:35
 * @version v1.0
 */

// @Results({ @Result(location = "main.jsp", name = "success"),
// @Result(location = "login.jsp", name = "login"),
// @Result(location = "fail.jsp", name = "failed") })
public class LoginAction extends BaseAction {
	@Autowired(required = false)
	private UserService userService;
	private User user;

	/**
	 * 设置user
	 * 
	 * @param user
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * 注意Struts2 表单值获取时，需要getUser方法，否则只能获取第一个值， 获取user
	 * 
	 * @return user
	 */

	public User getUser() {
		return user;
	}

	public String login() throws SystemException {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		if (user == null) {
			return LOGIN;
		}
		criteria.add(Restrictions.eq("username", user.getUsername()));
		List list = userService.findUsersByUsername(criteria);
		Collection<String> errors = new ArrayList<String>();
		if (list != null && !list.isEmpty()) {
			User tmp = (User) list.iterator().next();
			if (user.getPassword().equals(tmp.getPassword())) {
				// 登录成功
				user = tmp;
				session.put(SystemConstant.SES_USER_LOGIN,
						SystemConstant.SES_PREFIX_USER + tmp.getId());
				return ACTION;
			} else {
				errors.add(getText("user.password.wrong"));
				setActionErrors(errors);
				return INPUT;
			}
		} else {
			errors.add(getText("user.username.notfound"));
			setActionErrors(errors);
			return INPUT;
		}
	}

	public String logout() {
		session.put(SystemConstant.SES_USER_LOGIN, null);
		return SUCCESS;
	}

	public String adminIndex() {
		// 进行链接等管理
		this.loadUserFromSession();
		return SUCCESS;
	}

	// /*
	// * (non-Javadoc)
	// *
	// * @see com.opensymphony.xwork2.ActionSupport#execute()
	// */
	// @Override
	// public String execute() throws Exception {
	// HttpSession session = null;
	// session.invalidate();
	// // TODO Auto-generated method stub
	// session.setAttribute("username", user.getUsername());
	// session.setMaxInactiveInterval(30 * 60);
	// return super.execute();
	// }
	public void prepareLogin() {

	}

	private void loadUserFromSession() {
		String userIdSaveStr = (String) session
				.get(SystemConstant.SES_USER_LOGIN);
		String userIdStr = userIdSaveStr
				.substring(SystemConstant.SES_PREFIX_USER.length());
		int userId = Integer.parseInt(userIdStr);
		this.user = userService.getUserById(userId);
	}
}
